<?php
require_once("Response.php");
class Answer{
	public $link;
	public $response;
	public function __construct(){
		$this->response = new Response();
		$this->link = create_connection();
	}

	public function get(){
		$innersql = "select id from answers";
		$condition = "";
		// var_dump($_POST['data']);
		$sort = null;
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$qid = intval(filter_sql($data['qid']));
			$page = intval(filter_sql($data['page']));
			$limit = intval(filter_sql($data['limit']));
			$title = filter_sql($data['title']);
			$sort = filter_sql($data['sort']);
			$inner = [];
			if($qid) $inner[] = " qst_id = $qid";
			if($title != null) $inner[] = " content like '%$title%'";
			$arrlength=count($inner);
			if($arrlength) $condition .= ' where';
			// var_dump($inner);
			for($x=0;$x<$arrlength-1;$x++)
				$condition .= $inner[$x] . ' and';
			if($arrlength>=1) $condition .= $inner[$arrlength-1];
			$condition .= " order by id";
			if($sort == '-id') $condition .= " desc";
			$innersql .= $condition;
			if($page && $limit) $innersql = $innersql . " limit ".($limit * ($page - 1)).",$limit";
		}else{
			$innersql .= " order by id limit 0,20 ";
		}
		// echo $innersql;
		$sql = "select count(id) as total from answers" . $condition;
		$result=@execute_sql($this->link,'zhihu',$sql);
		if($result) {
			$total = intval(mysqli_fetch_assoc($result)['total']);
			$sql = "select a.id as id,qst_id,users_id as user_id,username,content,agree,disagree,addtime,a.status 
			       from answers a
			       inner join ($innersql) as temp_table using(id)
			       left join users u on a.users_id = u.id order by id";
			if($sort == '-id') $sql .= " desc";
			// echo $sql;
			$result=@execute_sql($this->link,'zhihu',$sql);
			if($result){
				$data = array();//初始化数组;
			    while ($row = mysqli_fetch_assoc($result)) {
			    	$alter = new AnswerData();//实例化对象;
					$alter->id = $row['id'];
					$alter->qst_id = $row['qst_id'];
					$alter->user_id = $row['user_id'];
					$alter->username = $row['username'];
					$alter->content = $row['content'];
					$alter->agree = $row['agree'];
					$alter->addtime = $row['addtime'];
					$alter->disagree = $row['disagree'];
					$alter->status = $row['status'];
					$data[] = $alter;
				}
				$this->response->returnData(200,'success',["items"=>$data,"total"=>$total]);
			}else {
				$this->response->returnError();
			}
		} else {
			$this->response->returnError();
		}
	}

	public function delete(){
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$id = filter_sql($data['id']);
			$sql = "delete from answers where id=$id";
			// echo $sql;
			if($result = @execute_sql($this->link,'zhihu',$sql)){
				$this->response->returnData(200,'success',null);
			}else{
				$this->response->returnError();
			}
		}else {
			$this->response->returnError();
		}
	}

	public function prohibit(){
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$id = filter_sql($data['id']);
			$status = filter_sql($data['status']);
			$sql = "update answers set status='$status' where id=$id";
			// echo $sql;
			if($result = @execute_sql($this->link,'zhihu',$sql)){
				$this->response->returnData(200,'success',null);
			}else{
				$this->response->returnError();
			}
		}else {
			$this->response->returnError();
		}
	}
}
class AnswerData
{
	public $id;
	public $qst_id;
	public $user_id;
	public $username;
	public $content;
	public $agree;
	public $disagree;
	public $addtime;
	public $status;
}